#include "logindialog.h"
#include "ui_logindialog.h"
#include"mainwindow.h"
#include<QMessageBox>
#include "userdao.h"
LoginDialog::LoginDialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::LoginDialog)
{
    ui->setupUi(this);

}

LoginDialog::~LoginDialog()
{
    delete ui;
}

void LoginDialog::on_pushButton_login_clicked()
{
    QString username = ui->lineEdit_usename->text();
    QString password = ui->lineEdit_password->text();
    if(username == "" || password =="")
    {
        QMessageBox::information(this,"warning","not null");
        return;
    }
    UserEntity ue;
    bool ok;
    ue.name = username;
    ue.password=password;
    UserDao::getInstance()->selectUser(ue,ok);

    if(ok)
    {
        MainWindow *lp=new MainWindow();
        lp->show();
    }
    else
    {
        QMessageBox::information(this,"reminder","login failed");
    }
}

void LoginDialog::on_pushButton_register_clicked()
{
    QString username = ui->lineEdit_usename->text();
    QString password = ui->lineEdit_password->text();
    if(username == "" || password =="")
    {
        QMessageBox::information(this,"warning","not null");
        return;
    }
    UserEntity ue;
    ue.name = username;
    ue.password=password;

    bool ok;
    UserDao::getInstance()->insertUser(ue,ok);
    if(ok)
    {
        QMessageBox::information(this,"reminder","register successful!!!");
        return;
    }
    else
    {
        QMessageBox::warning(this,"warn","register failed");
        return;
    }
}
